package com.ebring.shareEducation.service.impl;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.ebring.shareEducation.bean.Task;
import com.ebring.shareEducation.dao.TaskMapper;
import com.ebring.shareEducation.service.TaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;




import com.ebring.shareEducation.utils.RspMsg;
import com.ebring.shareEducation.utils.UuidUtils;


@Service
@Transactional
public class TaskServiceImpl implements TaskService {

	@Autowired
	private TaskMapper taskMapper;


	// 查询所有的菜单
	@Override
	public RspMsg<Task> selectTask() {
		RspMsg rspMsg = new RspMsg();
		Map<String, Object> map = new LinkedHashMap<>();
		List<Task> list=taskMapper.selectAll();
		if (list.isEmpty() || list == null) {
			rspMsg.setOK();
			rspMsg.setMsg("未找到相关任务");
			return rspMsg;
		}
		map.put("tasklist",list);
		rspMsg.setOK();
		rspMsg.setResult(map);
		return rspMsg;
	}

	// 修改菜单
	@Override
	public RspMsg<Task> updateTask(Task task) {
		RspMsg rspMsg = new RspMsg();
		int i = taskMapper.updateByPrimaryKeySelective(task);
		if (i == 0) {
			rspMsg.setError();
			rspMsg.setMsg("修改失败");
			return rspMsg;
		}
		rspMsg.setOK();
		return rspMsg;

	}

	// 根据ID删除任务

	@Override
	public RspMsg<Task> deleteById(String taskId) {
		RspMsg rspMsg = new RspMsg();
		int i = taskMapper.deleteByPrimaryKey(taskId);
		if (i == 0) {
			rspMsg.setError();
			rspMsg.setMsg("删除失败");
			return rspMsg;
		}

		rspMsg.setOK();
		return rspMsg;
	}

	// 新增任务
	@Override
	@Transactional
	public RspMsg<Task> insertTask(Task task) {
		RspMsg rspMsg = new RspMsg();
		task.setTaskId(UuidUtils.getNumberId(6));
		int i = taskMapper.insertSelective(task);
		if (i == 0) {
			rspMsg.setError();
			rspMsg.setMsg("新增菜单失败");
			return rspMsg;
		}
		rspMsg.setOK();
		return rspMsg;
	}


	// 查询用户的任务
	@Override
	public RspMsg<Task> selectUTask(String taskId) {
		RspMsg rspMsg = new RspMsg();
		Map<String, Object> map = new LinkedHashMap<>();
		List<Task> list =null;
		if(taskId !=null){
			list = taskMapper.selectUTask(taskId);
			if (list.isEmpty() || list == null) {
				rspMsg.setOK();
				rspMsg.setMsg("未找到相关任务");
				return rspMsg;
			}
			map.put("tasklist",list);
			rspMsg.setOK();
			rspMsg.setResult(map);
			return rspMsg;
		}

		rspMsg.setOK();
		rspMsg.setMsg("未获取参数");
		return rspMsg;
	}

}
